<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Role;
use App\Http\Model\Permission;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class RoleController extends Controller
{
    public function roleshow($id)
    {
        $roleshow = Role::find($id);
        //dd($roleshow);
       $roles = Permission::get();

        return view('admin.roleshow.add',compact('roleshow','roles'));
    }

    
    public function add(Request $request)
    {

        $input = $request->except('_token');
        if($input){
            foreach($input['role_id'] as $k=>$v){
                \DB::insert('insert into permissions_user (role_id, perm_id) values (?, ?)', [$input['id'], $v]);
            }
            return "添加成功！";
        }else{
            return "添加失败！";
        }
    }
    

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $role = Role::get();
        return view('admin.role.index',['data'=>$role]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $input = Input::except('_token');
        $re = Role::create($input);
            if($re){
                return redirect('admin/role');
            }else{
                return back()->with('errors','添加失败');
            }
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data =  Role::where('id',$id)->first();
        //可以使用compact函数给模板传变量
        return view('admin.role.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $role = Role::find($id);
        $role->name = $request->input('name');
        $re = $role->save();
        if($re){
            return redirect('admin/role');
        }else{
            return back()->with('errors','用户修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $re =   Role::where('id',$id)->delete();
        if($re){
            $data = [
                'status'=>0,
                'msg'=>'用户删除成功'
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'用户删除失败'
            ];
        }
        return $data;
    }
}
